package com.imy.accessibility.node;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatCheckBox;

import com.imy.accessibility.R;
import com.imy.common.base.BaseCustomViewGroup;
import com.imy.common.util.SoftInputUtils;
import com.imy.common.util.ToastUtil;
import com.imy.lua.lib.node.UDRule;

public class FindByView extends BaseCustomViewGroup {
    private TextView searchTv;
    private TextView cancelTv;
    private EditText textEt;
    private EditText idEt;
    private EditText classEt;
    private AppCompatCheckBox textBox;
    private AppCompatCheckBox idBox;
    private AppCompatCheckBox classBox;

    public FindByView(Context context) {
        super(context);
    }

    public FindByView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FindByView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(View inflate) {
        cancelTv = inflate.findViewById(R.id.cancel_tv);
        searchTv = inflate.findViewById(R.id.search_tv);
        textEt = inflate.findViewById(R.id.find_by_text_edit);
        idEt = inflate.findViewById(R.id.find_by_id_edit);
        classEt = inflate.findViewById(R.id.find_by_class_edit);
        textBox = inflate.findViewById(R.id.find_by_text_check_box);
        idBox = inflate.findViewById(R.id.find_by_id_check_box);
        classBox = inflate.findViewById(R.id.find_by_class_box);
        inflate.findViewById(R.id.find_by_content_ll).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //空方法
            }
        });
        cancelTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(GONE);
                if (listener != null) {
                    listener.onCancel();
                }
            }
        });
        searchTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!idBox.isChecked()
                        && !textBox.isChecked() && !classBox.isChecked()) {
                    ToastUtil.toast(getContext(), R.string.please_choose_one);
                    return;
                }
                UDRule udRule = new UDRule();
                if (textBox.isChecked() &&
                        !textEt.getText().toString().equals("")) {
                    udRule.text(textEt.getText().toString());
                }
                if (idBox.isChecked() &&
                        !idEt.getText().toString().equals("")) {
                    udRule.id(idEt.getText().toString());
                }
                if (classBox.isChecked() &&
                        !classEt.getText().toString().equals("")) {
                    udRule.className(classEt.getText().toString());
                }
                if (listener != null) {
                    listener.onSearch(udRule);
                }
                setVisibility(GONE);
            }
        });
    }

    public void show() {
        setVisibility(VISIBLE);
        SoftInputUtils.showInput(textEt, mContext);
    }

    public void hide(){
        setVisibility(GONE);
        SoftInputUtils.hideInput(textEt,mContext);
    }
    @Override
    protected int getLayoutID() {
        return R.layout.custom_view_find_by;
    }

    public interface FindByListener {
        void onCancel();

        void onSearch(UDRule udRule);
    }

    public void setFindByListener(FindByListener findByListener) {
        this.listener = findByListener;
    }

    private FindByListener listener;
}
